package com.android.gufengmall.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.ForumBean;
import com.android.gufengmall.utils.BitMapConversionUtils;
import com.bumptech.glide.Glide;

import java.util.List;

public class ForumAdapter extends RecyclerView.Adapter<ForumAdapter.ViewHolder> {

    private static final String TAG = "ForumAdapter";

    private Context mContext;
    private List<ForumBean> mForumBeans;
    private IDeleteLongClick mIDeleteLongClick;

    public ForumAdapter(Context context, List<ForumBean> forumBeans) {
        mContext = context;
        mForumBeans = forumBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_my_forum,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ForumBean forumBean = mForumBeans.get(position);
        holder.mTvTitle.setText(forumBean.getTitle());
        holder.mTvInfo.setText(forumBean.getInfo());
        holder.mTvUserName.setText(forumBean.getUserName());
        holder.mTvDate.setText(forumBean.getDate());
        Glide.with(mContext).load(BitMapConversionUtils.stringToBitmap(forumBean.getUserPhoto()))
                .dontAnimate().error(R.drawable.icon_photo).fitCenter().into(holder.mImgPhoto);
        holder.itemView.setOnClickListener(v -> {
            mIDeleteLongClick.onClickForum(position);
        });
        holder.itemView.setOnLongClickListener(v -> {
            mIDeleteLongClick.onLongClickDelete(position);
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return mForumBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvTitle;
        private final TextView mTvInfo;
        private final TextView mTvUserName;
        private final TextView mTvDate;
        private final ImageView mImgPhoto;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvTitle = itemView.findViewById(R.id.tv_title);
            mTvInfo = itemView.findViewById(R.id.tv_info);
            mTvUserName = itemView.findViewById(R.id.tv_userName);
            mTvDate = itemView.findViewById(R.id.tv_date);
            mImgPhoto = itemView.findViewById(R.id.img_photo);
        }
    }

    public void setIDeleteLongClick(IDeleteLongClick longClick) {
        mIDeleteLongClick = longClick;
    }

    public interface IDeleteLongClick{
        void onLongClickDelete(int position);

        void onClickForum(int position);
    }
}
